package design.simdi.code.final_paper.site.panels.administrare.absolventi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.daos.PersonDao;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.enums.UserTypes;
import design.simdi.code.final_paper.site.application.ApplicationRoles;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.site.panels.administrare.SortablePersonDataProvider;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 8:11:32 PM)
 * 
 */

public class AbsolventiTabPanel extends PagePanel {

	private static final long serialVersionUID = 1L;
	@SpringBean
	private PersonDao personDao;

	public AbsolventiTabPanel(String id) {
		super(id);
		setOutputMarkupId(true);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);
	}

	@Override
	public void panelInit() {
		List<IColumn<Person>> columns = new ArrayList<IColumn<Person>>();

		columns.add(new PropertyColumn<Person>(new ResourceModel("nume"), "nume", "nume"));
		columns.add(new PropertyColumn<Person>(new ResourceModel("prenume"), "prenume", "prenume"));

		Roles roluriCurente = MyAuthenticatedWebSession.get().getRoles();
		if (MyAuthenticatedWebSession.get().isSignedIn()
				&& roluriCurente.containsAll(Arrays.asList(new String[] { ApplicationRoles.ADMIN }))) {

			columns.add(new PropertyColumn<Person>(new ResourceModel("user.login"), "user.login", "user.login"));
			columns.add(new AbstractColumn<Person>(new ResourceModel("user.status"), "user.status") {

				private static final long serialVersionUID = 1L;

				public void populateItem(Item<ICellPopulator<Person>> cellItem, String componentId,
						IModel<Person> rowModel) {
					cellItem.add(new Label(componentId, new ResourceModel(rowModel.getObject().getUser().getStatus()
							.getName())));
				}
			});
		}
		columns.add(new PropertyColumn<Person>(new ResourceModel("user.email"), "user.email", "user.email"));
		columns.add(new PropertyColumn<Person>(new ResourceModel("absolvent.anAbsolvire"), "absolvent.anAbsolvire",
				"absolvent.anAbsolvire"));

		SortablePersonDataProvider sortablePersonDataProvider = new SortablePersonDataProvider();
		// sortablePersonDataProvider.getCriterii().add(Restrictions.isNotNull("absolvent"));
		sortablePersonDataProvider.setTipUtilizator(UserTypes.ABSOLVENT);

		add(new MyDataTable<Person>("table", columns, sortablePersonDataProvider, getRowsPerPage()) {

			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new AbsolventActionToolbar(this);
			}
		});
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

}
